/*
 * Records package
 */
package edu.gatech.oad.records;

import javax.swing.DefaultListModel;

import edu.gatech.oad.doctorsorder.DoctorsOrder;
import edu.gatech.oad.doctorsorder.LabOrder;

/**
 * Adds and removes TreatmentRecords and LabResults from
 * both the PatientHistory and the corresponding 
 * TreatmentRecord if necessary
 * @author Nathan
 * @version 1.0
 */
public class PatientHistoryController {
	/**
	 * PatientHistory to add and remove records
	 */
	private final PatientHistory curHistory;
	
	/**
	 * Constructs the controller
	 * @param curHistory the PatientHistory to manipulate
	 */
	public PatientHistoryController(PatientHistory curHistory){
		this.curHistory = curHistory;
	}
	
	/**
	 * Removes LabResults from the PatientHistory and
	 * the TreatmentRecords DoctorsOrders
	 * @param resultToRemove the lab to remove
	 */
	public void removeLab(LabResults resultToRemove){
		boolean found = false;
		curHistory.removeLabRecord(resultToRemove);
		for(TreatmentRecord t : curHistory.getTreatmentRecords()){//remove lab from the TreatmentRecord
			for(DoctorsOrder l : t.getDoctorsOrders()){
				if(resultToRemove.getOrder().equals(l)){
					found = true;
					t.getDoctorsOrders().remove(l);
					break;
				}
			}
			if(found){
				break;
			}
		}
	}
	
	/**
	 * Removes a TreatmentRecord and all of the LabResults
	 * associated with it
	 * @param toRemove the TreatmentRecord to remove
	 * @param labModel the ListModel to remove elements from
	 * on the PatientHistoryPanel
	 */
	public void removeTreatRec(TreatmentRecord toRemove, DefaultListModel labModel){
		curHistory.removeTreatmentRecord(toRemove);
		for(DoctorsOrder l : toRemove.getDoctorsOrders()){
			if(l instanceof LabOrder){
				labModel.removeElement(((LabOrder) l).getLabResult()); // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.unusedReturnValue
				curHistory.removeLabRecord(((LabOrder) l).getLabResult());
			}
		}
	}
	
	/**
	 * @return String representation of the controller
	 */
	public String toString(){
		return "PatientHistoryController for the PatientHistory: " +
			curHistory.toString();
	}
}
